package com.wang.lottery.domain.rule.service.engine.impl;

import com.wang.lottery.domain.rule.model.aggregates.TreeRuleRich;
import com.wang.lottery.domain.rule.model.req.DecisionMatterReq;
import com.wang.lottery.domain.rule.model.res.EngineResult;
import com.wang.lottery.domain.rule.model.vo.TreeNodeVO;
import com.wang.lottery.domain.rule.repository.IRuleRepository;
import com.wang.lottery.domain.rule.service.engine.EngineBase;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author wjh
 * @create 2023-11-11 17:49
 * 规则引擎处理器
 */
@Service
public class RuleEngineHandle extends EngineBase {
    
    @Resource
    private IRuleRepository ruleRepository;
    
    /**
     * 规则过滤器接口
     *
     * @param matter 规则决策物料
     * @return 规则决策结果
     */
    @Override
    public EngineResult process(DecisionMatterReq matter) {
        //先获取规则树
        TreeRuleRich treeRuleRich = ruleRepository.queryTreeRuleRich(matter.getTreeId());
        if (treeRuleRich == null) {
            throw new RuntimeException("Tree Rule is null!");
        }
        TreeNodeVO treeNodeVO = super.engineDecisionMaker(treeRuleRich, matter);
        EngineResult engineResult = new EngineResult();
        engineResult.setSuccess(true);
        engineResult.setTreeId(matter.getTreeId());
        engineResult.setUserId(matter.getuId());
        engineResult.setNodeId(treeNodeVO.getId());
        engineResult.setNodeValue(treeNodeVO.getNodeValue());
        return engineResult;
    }
}
